package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Author ShiJieLin
 * @Date 2022/3/30 20:07
 * @Version 1.0
 */
@Configuration
public class GuliMallCorsConfig {


    @Bean
    public CorsWebFilter corsWebFilter(){
        // 1.因为gateway使用的是：reactive  CorsConfigurationSource
        UrlBasedCorsConfigurationSource ubccs = new UrlBasedCorsConfigurationSource();
        // 2.创建对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //1、配置跨域
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);
        // 3.注册
        ubccs.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(ubccs);
    }

}
